package com.xbongbong.parent.abstracts;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.elasticsearch.util.ElasticSearchConditionUtil;
import com.xbongbong.paas.elasticsearch.util.EsHelper;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.help.BusinessUserTeamHelp;
import com.xbongbong.paas.help.SharePermissionHelp;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.parent.interfaces.BusinessPermissionService;
import com.xbongbong.pro.statistic.result.pojo.ElasticAggsPojo;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.sys.help.ProPermissionHelp;
import com.xbongbong.sys.model.UserModel;
import org.elasticsearch.index.query.BoolQueryBuilder;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 * @author 吴峰
 * @date 2022/05/18 17:08
 */
public abstract class AbstractBusinessPermissionService<D, F> implements BusinessPermissionService<D, F> {

    @Resource
    private EsHelper esHelper;

    @Resource
    private UserModel userModel;
    @Resource
    protected BusinessUserTeamHelp businessUserTeamHelp;
    @Resource
    protected SharePermissionHelp sharePermissionHelp;


    protected List<String> getUserIdIn(UserVO loginUser) throws XbbException {


        Integer dataPermission = userModel.getDataPermission(loginUser, false);
        return userModel.getSubIdListByDataPermission(dataPermission, loginUser.getUserId(), loginUser.getCorpid(), BasicConstant.ZERO,false);
    }


    @Override
    public boolean checkDelPermission(List<String> userIdIn, D paasFormDataEntityExt, Integer businessType) throws XbbException {
        return true;
    }

    @Override
    public void checkDataEditPermission(F formEntity, D paasFormDataEntityExt, UserVO loginUser, Integer subBusinessType) throws XbbException {
        return;
    }

    @Override
    public Boolean checkPermissionByTeam(String corpid, Integer businessType, BoolQueryBuilder boolQueryBuilder) throws XbbException {
        return false;
    }

    protected boolean checkPermissionByTeam(BoolQueryBuilder boolQueryBuilder, IndexTypeEnum indexTypeEnum) throws XbbException {
        ElasticAggsPojo elasticAggsPojo = esHelper.elasticAggs(indexTypeEnum, boolQueryBuilder, null);
        return StringUtil.toInt(elasticAggsPojo.getTotal(), 0) > 0;
    }
    /**
     * 校验团队权限
     * @param corpid
     * @param dataId
     * @param loginUser
     * @param indexTypeEnum
     * @param subBusinessType
     * @throws XbbException
     */
    protected boolean validatePermission(String corpid, Long dataId, UserVO loginUser, IndexTypeEnum indexTypeEnum, Integer subBusinessType) throws XbbException {
        List<String> userIdIn = getUserIdIn(loginUser);
        BoolQueryBuilder boolQueryBuilder = ElasticSearchConditionUtil.permissionQuery(dataId, userIdIn, corpid, indexTypeEnum);
        return checkPermissionByTeam(corpid, subBusinessType, boolQueryBuilder);
    }

    /**
     * 校验团队权限
     * @param corpid
     * @param dataId
     * @param loginUser
     * @param indexTypeEnum
     * @param subBusinessType
     * @throws XbbException
     */
    protected boolean hasPermission(String corpid, Long dataId, UserVO loginUser, IndexTypeEnum indexTypeEnum, Integer subBusinessType) throws XbbException {
        List<String> userIdIn = getUserIdIn(loginUser);
        BoolQueryBuilder boolQueryBuilder = ElasticSearchConditionUtil.permissionQuery(dataId, userIdIn, corpid, indexTypeEnum);
        return checkPermissionByTeam(corpid, subBusinessType, boolQueryBuilder);
    }

    /**
     * 拼装负责团队进data
     *
     * @param paasFormDataEntityExt
     * @param businessType
     */
    protected void setUserTeamIntoData(PaasFormDataEntityExt paasFormDataEntityExt, Integer businessType){
        Long dataId = paasFormDataEntityExt.getId();
        String corpid = paasFormDataEntityExt.getCorpid();
        Map<Long, List<String>> ownerIdMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<Long, List<String>> coUserIdMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        businessUserTeamHelp.getUserIdMapSetShow(new HashSet<>(), Collections.singletonList(dataId), corpid, businessType, false, ownerIdMap, coUserIdMap, DelEnum.NORMAL.getDel());
        paasFormDataEntityExt.getData().put(FieldTypeEnum.OWNERID.getAlias(), ownerIdMap.get(dataId));
        paasFormDataEntityExt.getData().put(FieldTypeEnum.COUSERID.getAlias(), coUserIdMap.get(dataId));
    }
}
